<template>
    <div class="addeditdictionary">
        <el-dialog title="新增字典项" width="30%" :visible.sync="approvalVisible">
            <div class="item-complete">
                <span>所属系统：</span>
                <el-autocomplete
                    size="mini"
                    v-model="formInline.sys"
                    :fetch-suggestions="querySysList"
                    placeholder="请输入"
                ></el-autocomplete>
            </div>
            <div class="item-complete">
                <span>父编码：</span>
                <el-autocomplete
                    size="mini"
                    v-model="formInline.parentCode"
                    :fetch-suggestions="getParentCode"
                    placeholder="请输入父编码"
                ></el-autocomplete>
            </div>
            <div class="item-complete">
                <span>编码：</span>
                <el-input  size="mini" v-model="formInline.code" placeholder="请输入内容"></el-input>
            </div>
            <div class="item-complete">
                <span>字典值：</span>
                <el-input size="mini" v-model="formInline.value" placeholder="请输入内容"></el-input>
            </div>
            <div class="item-complete">
                <span>排序：</span>
                <el-input  size="mini" v-model="formInline.order" placeholder="请输入内容"></el-input>
            </div>
            <div class="item-complete">
                <span>是否软删：</span>
                <el-switch
                    v-model="formInline.deleteFlag"
                    active-color="#13ce66"
                    inactive-color="#c2bdbd"
                    active-text="是"
                    inactive-text="否"
                    active-value="1"
                    inactive-value="0">
                </el-switch>
            </div>
            <div class="item-complete">
                <span>参数：</span>
                <el-input  
                    type="textarea"
                    :autosize="{ minRows: 3 }"
                    v-model="formInline.params" 
                    placeholder="请输入内容">
                </el-input>
            </div>            
            <span slot="footer" class="dialog-footer">
                <el-button 
                    size="mini" 
                    type="primary" 
                    v-if="formInline.dictId" 
                    @click="deleteThis(formInline.dictId)"
                >删 除</el-button>
                <el-button size="mini" type="primary" @click="submitData()">保 存</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import Vue from "vue";
import { Autocomplete } from "element-ui";
import { alertMessage } from "@/config/myUtil";
import { sysListDataApi ,addDictionaryListApi ,getParentCodeApi ,updataDictionaryApi } from "@/api/getData";
import { parse } from 'path';
Vue.use(Autocomplete);
export default {
    name:"addeditdictionary",
    data(){
        return {
            approvalVisible:false,
            dataType:"add",
            formInline:{}
        }
    },
    methods:{
        async querySysList(queryString,cb){
            try {
                let data = await sysListDataApi({
                    key: queryString || ""
                });
                if (cb) {
                    var list = data.map(el => {
                        return {
                            key: el,
                            value: el
                        };
                    });
                    cb(list);
                } else {
                    this.formInline.sys = "";
                }
            } catch (error) {
                cb([]);
            }
        },
        async getParentCode(queryString,cb){
            try {
                if(!this.formInline.sys){
                    alertMessage("请选择系统","warning");
                    cb([]);
                    return
                }
                let data = await getParentCodeApi({
                    sys: this.formInline.sys,
                    key: queryString || ""
                });
                if (cb) {
                    var list = data.map(el => {
                        return {
                            key: el,
                            value: el
                        };
                    });
                    cb(list);
                } else {
                    this.topviewName = "";
                }
            } catch (error) {
                cb([]);
                console.log(error)
            }
        },
        async submitData(){
            try {
                let data = null;
                if(this.dataType == "add"){
                    data = await addDictionaryListApi(this.formInline);
                }else{
                    var params = JSON.parse(JSON.stringify(this.formInline));
                    data = await updataDictionaryApi(params);
                }
                if(data){
                    alertMessage("提交成功。");
                    this.$emit("updataList");
                }
                this.approvalVisible = false;
            } catch (error) {
                console.log(error)
            }
        },
        changeStatus(item){
            if(item){
                this.formInline = item;
                this.dataType = "edit";
            }else{
                this.dataType = "add";
                this.formInline = {
                    sys:"",
                    code:"",
                    value:"",
                    order:"",
                    params:"",
                    deleteFlag:"0",
                    parentCode:""
                }
            };
            this.approvalVisible = true;
        },
        deleteThis(id){
            this.$emit("deleteThis",id)
        }
    }
}
</script>
<style lang="less" scoped>
@import url('../less/addeditdictionary.less');
</style>